package com.github.binarywang.demo.wx.cp.controller;

import com.demo.third.ThirdAuthenticationDemo;
import com.github.binarywang.demo.wx.cp.error.BizException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * created by lanxinghua@2dfire.com on 2020/2/17
 * 企业微信跳转入口
 */
@RestController
@RequestMapping(value = "/work-wechat/jump/v1")
public class EnterpriseWeChatJumpController extends ThirdBaseController {
    private static final Logger logger = LoggerFactory.getLogger(EnterpriseWeChatJumpController.class);
    private String baseOauthUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=%s#wechat_redirect";
    private String oauthUrl = "http://chenxingxing.51vip.biz/oauth";


    @RequestMapping(value = "/split", method = RequestMethod.GET)
    public void split(
        @RequestParam(value = "url", required = true) String url,
        // @RequestParam(value = "xtoken", required = true) String token,
        HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        if (StringUtils.isEmpty(url)){
            throw new BizException("url为空");
        }
        response.sendRedirect(buildUrl(url));
        return;
    }


    /**
     * 构建授权Url
     * @param url
     * @return
     */
    private String buildUrl(String url){
        String resultUrl = String.format(baseOauthUrl, suiteID, oauthUrl, url);
        logger.info("网页授权url:" + resultUrl);
        return resultUrl;
    }
}
